# -*- coding: utf-8 -*-
# @Time : 2021/9/11 16:30
# @Author : 阿怪
# @Email : 673691277@qq.com
# @File : client_page.py
# @Project : crm自动化测试项目实战
'''客户页面类'''

from selenium.webdriver.common.by import By

from page.base_page import BasePage
from time import sleep

import time
from selenium.webdriver.support.select import Select
today = time.strftime('%Y-%m-%d')  # 获取当前日期
from utils.Echo_defs.test_random import get_data
from utils.util import HOST2
class ClientPage(BasePage):
    '''客户页面类'''

    #定义定位器
    url = '{}/index.php?m=customer&a=index'.format(HOST2)
    check_all_locator = (By.ID,'check_all')  #全选按钮定位器
    operation_locator = (By.CSS_SELECTOR,'body > div.container > div.row > div:nth-child(1) > ul > div > a')  #批量操作定位器
    delete_all_loactor = (By.XPATH,'/html/body/div[5]/div[2]/div[1]/ul/div/ul/li[1]/a')
    add_client_locator =(By.CSS_SELECTOR,'body > div.container > div.row > div:nth-child(1) > div > a')  #添加客户按钮定位器
    add_industry_locator=(By.ID,'industry')   #添加客户界面，添加行业
    select_department_locator = (By.ID,'department')   #选择部门定位器
    select_role_locator = (By.ID,'role')    #选择人员定位器
    start_time_locator = (By.ID,'start_time')     #开始日期定位器
    end_time_locator = (By.ID,'end_time')       #结束日期
    search_locator = (By.XPATH,'/html/body/div[5]/div[1]/ul/li[5]/a')    #搜索按钮定位器
    client_total_locator = (By.XPATH,'/html/body/div[5]/div[1]/ul/li[5]/a')   #客户统计定位器
    client_locator = (By.XPATH,'/html/body/div[5]/div[1]/ul/li[1]/a')   #[客户]首页定位器
    get_text_locator = (By.XPATH,'//*[@id="report_content"]/table/tbody/tr/td[2]')    #搜索线索数量定位器
    industry_total_locator = (By.XPATH,'//*[@id="show_industry"]')    #【客户行业统计】定位器
    industry_text_locator = (By.CSS_SELECTOR,'#highcharts-6 > svg > text.highcharts-title') #行业统计文本定位器



    def click_addclient(self):
        '''点击【添加客户】'''
        element = self.driver.find_element(*self.add_client_locator)
        element.click()

    def input_client(self,name):
        '''输入客户名'''
        element1 = self.driver.find_element(By.ID, 'name')
        element1.send_keys(name+str(get_data()))  # 这里这个客户名不能重复，每一次都要改变。。、

    def choice_industry(self, industryname='对外贸易'):
        '''客户行业选择对外贸易'''
        locator = self.driver.find_element(*self.add_industry_locator)
        select = Select(locator)
        select.select_by_visible_text(industryname)  # 客户行业选择对外贸易

    def click_submit_client(self):
        '''点击保存'''
        element2 = self.driver.find_element(By.NAME, 'submit')
        element2.click()
        sleep(1)

    def add_client(self,name):
        self.click_addclient()
        self.input_client(name)
        self.choice_industry()
        self.click_submit_client()


    def getin_total_client(self):
        '''点击进入客户统计界面'''
        element = self.driver.find_element(*self.client_total_locator)
        element.click()
        sleep(1)

    #搜索操作
    def choice_department(self,value='1'):
        '''选择部门-办公室'''
        locator = self.driver.find_element(*self.select_department_locator)
        select = Select(locator)
        select.select_by_value(value)

    def choice_role(self, role='CEO-admin'):
        '''选择员工-admin'''
        locator2 = self.driver.find_element(*self.select_role_locator)
        select2 = Select(locator2)
        select2.select_by_visible_text(role)

    def choicestart_time(self):
        """开始日期输入今天"""
        start_time_element = self.driver.find_element(*self.start_time_locator)
        start_time_element.send_keys(today)

    def choiceend_time(self):
         '''结束日期输入今天'''
         end_time_element = self.driver.find_element(*self.end_time_locator)
         end_time_element.send_keys(today)

    def input_search(self):
        '''搜索条件处理'''
        self.choice_department()
        self.choice_role()
        self.choicestart_time()
        self.choiceend_time()

    def search_submit(self):
        '''客户统计界面点击搜索'''
        element = self.driver.find_element(*self.search_locator)
        element.click()

    def get_text(self):
        '''获取搜索后的数量'''
        text = self.driver.find_element(*self.get_text_locator).text
        return text

    def industry_check(self):
        '''进入客户行业统计界面'''
        element = self.driver.find_element(*self.industry_total_locator)
        element.click()

    def get_industry_text(self):
        '''获取客户行业统计页面的信息'''
        text = self.driver.find_element(*self.industry_text_locator).text
        return text

    #删除所有客户数据
    def click_client(self):
        '''点击【客户】'''
        self.driver.find_element(*self.client_locator).click()

    def click_checkall(self):
        '''点击【全选】'''
        element1 = self.driver.find_element(*self.check_all_locator)
        element1.click()  # 点击全选按钮

    def click_operation(self):
        '''点击【批量操作】'''
        element2 = self.driver.find_element(*self.operation_locator)
        element2.click()  # 点击批量操作
        sleep(3)

    def click_deleteall(self):
        '''点击批量删除'''
        element3 = self.driver.find_element(*self.delete_all_loactor)
        element3.click()  # 点击批量删除

    def clear_client(self):
        '''删除所有客户数据'''
        self.click_client()
        self.click_checkall()
        self.click_operation()
        self.click_deleteall()
        self.alert()








